package com.ruibo.modules.utils;

import com.ruibo.tax.constants.SendDataPackageConstants;
import com.ruibo.tax.constants.TaxConstants;
import com.ruibo.tax.model.HBJHHBCF;
import com.ruibo.tax.model.HBJHJCZDJC;
import com.ruibo.tax.model.HBJHXKZJBXX;
import com.ruibo.tax.model.base.PackageHead;
import com.ruibo.tax.model.base.SendBasic;
import com.ruibo.tax.model.base.XMLEncodeModel;
import com.ruibo.tax.model.log.TaxSendLog;
import com.ruibo.tax.mq.send.ReconciliationSendZIPMessage;
import com.ruibo.tax.repository.HBJHXKZJBXXRepository;
import com.ruibo.tax.repository.TaxSendLogRepository;
import com.ruibo.tax.utils.PathZip;
import com.ruibo.tax.utils.XMLUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.io.File;
import java.nio.charset.Charset;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Component
public class SendDataUtil {

    @Autowired
    private XMLUtils xmlUtils;

    @Value("${baseFilePath}")
    private String baseFilePath;

    @Value("${beiFenFilePath}")
    private String beiFenFilePath;

    @Autowired
    private ReconciliationSendZIPMessage reconciliationSendZIPMessage;

    @Autowired
    private TaxSendLogRepository taxSendLogRepository;

    @Autowired
    private PathZip pathZip;


    public void sendPWXKDataToMQ(List<HBJHXKZJBXX> list) {
        XMLEncodeModel<HBJHXKZJBXX> model = new XMLEncodeModel<HBJHXKZJBXX>();
        XMLEncodeModel<HBJHXKZJBXX> PWXKZBData = xmlUtils.data(model, list, SendDataPackageConstants.PWXKZB);
        String msg = null;
        try {
            msg = PWXKZBData.toSendData(Charset.forName("GBK"));
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        String sjbbh = PWXKZBData.getHeads().getSJBBH();
        String sjblx = PWXKZBData.getHeads().getSJBLX() + "_";
        log.info("开始创建文件");
        // 创建新的数据    生成xml文件
        try {
            //创建xml文件
            xmlUtils.creteFile(msg, PWXKZBData);
            // 压缩文件路径
            String dataPackage = beiFenFilePath + SendDataPackageConstants.PWXKZB + "/" + sjbbh + ".zip";
            // 执行任务之前  压缩文件
            backupSourceFileToDestFile(baseFilePath + PWXKZBData.getHeads().getSJBLX() + "\\" + XMLUtils.HJBH + sjblx + sjbbh + ".xml", dataPackage);
            // 发送压缩数据
            reconciliationSendZIPMessage.sendZipMsgToQLHB2SW(dataPackage);
            PackageHead heads = PWXKZBData.getHeads();
            saveSendLog(heads, dataPackage);
        } catch (Exception e) {
            log.error("执行数据发送！,{}", e.toString());
        }

    }


    public void sendZDJCDataToMQ(List<HBJHJCZDJC> list) {
        XMLEncodeModel<HBJHJCZDJC> model = new XMLEncodeModel<HBJHJCZDJC>();
        XMLEncodeModel<HBJHJCZDJC> xmlEncodeModel = xmlUtils.data(model, list, SendDataPackageConstants.ZDJCXX);
        String msg = null;
        try {
            msg = xmlEncodeModel.toSendData(Charset.forName("GBK"));
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        PackageHead heads = xmlEncodeModel.getHeads();
        String sjbbh = heads.getSJBBH();
        String sjblx = heads.getSJBLX() + "_";
        log.info("开始创建文件");
        // 创建新的数据    生成xml文件
        try {
            //创建xml文件
            xmlUtils.creteFile(msg, xmlEncodeModel);
            // 压缩文件路径
            String dataPackage = beiFenFilePath + SendDataPackageConstants.ZDJCXX + "/" + sjbbh + ".zip";
            // 执行任务之前  压缩文件
            backupSourceFileToDestFile(baseFilePath + heads.getSJBLX() + "\\" + XMLUtils.HJBH + sjblx + sjbbh + ".xml", dataPackage);
            // 发送压缩数据
            reconciliationSendZIPMessage.sendZipMsgToQLHB2SW(dataPackage);

            saveSendLog(heads, dataPackage);
        } catch (Exception e) {
            log.error("执行数据发送！,{}", e.toString());
        }

    }

    public void sendHBCFDataToMQ(List<HBJHHBCF> list) {
        XMLEncodeModel<HBJHHBCF> model = new XMLEncodeModel<HBJHHBCF>();
        XMLEncodeModel<HBJHHBCF> xmlEncodeModel = xmlUtils.data(model, list, SendDataPackageConstants.HBCFXX);
        String msg = null;
        try {
            msg = xmlEncodeModel.toSendData(Charset.forName("GBK"));
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        PackageHead heads = xmlEncodeModel.getHeads();
        String sjbbh = heads.getSJBBH();
        String sjblx = heads.getSJBLX() + "_";
        log.info("开始创建文件");
        // 创建新的数据    生成xml文件
        try {
            //创建xml文件
            xmlUtils.creteFile(msg, xmlEncodeModel);
            // 压缩文件路径
            String dataPackage = beiFenFilePath + SendDataPackageConstants.HBCFXX + "/" + sjbbh + ".zip";
            // 执行任务之前  压缩文件
            backupSourceFileToDestFile(baseFilePath + heads.getSJBLX() + "\\" + XMLUtils.HJBH + sjblx + sjbbh + ".xml", dataPackage);
            // 发送压缩数据
            reconciliationSendZIPMessage.sendZipMsgToQLHB2SW(dataPackage);
            saveSendLog(heads, dataPackage);
        } catch (Exception e) {
            log.error("执行数据发送！,{}", e.toString());
        }

    }

    private void saveSendLog(PackageHead heads, String dataPackage) {
        TaxSendLog taxSendLog = new TaxSendLog();

        // 已发送  未收到回执的状态
        taxSendLog.setSendStatus(TaxConstants.UN_RECEIPT_STATUS);
        taxSendLog.setPkg_num(heads.getSJBBH());
        taxSendLog.setFile_path(dataPackage);
        taxSendLog.setPkg_type(heads.getSJBLX());
        taxSendLog.setUnit_code(heads.getDWDM());
        taxSendLog.setUnit_name(heads.getDWMC());
        taxSendLog.setRecord_num(heads.getJLS());
        taxSendLog.setStatus(TaxConstants.ACTIVE);
        taxSendLog.setTimes(1);
        taxSendLog.setCreateAt(LocalDateTime.now());
        taxSendLog.setUpdateAt(LocalDateTime.now());
        taxSendLogRepository.save(taxSendLog);
    }

    /**
     * 备份源xml文件
     *
     * @param sourceFile  原文件
     * @param dataPackage 备份文件
     */
    private void backupSourceFileToDestFile(String sourceFile, String dataPackage) {

        int index = dataPackage.lastIndexOf("/");
        String substring = dataPackage.substring(0, index + 1);
        try {
            // 创建备份文件路径
            File desFile = new File(substring);
            if (!desFile.exists()) {
                desFile.mkdirs();
            }
            //只能有一个线程执行
            synchronized (this) {
                // 执行备份压缩
                pathZip.zipFiles(sourceFile, dataPackage);
            }
        } catch (Exception e) {
            log.error("压缩文件失败！");
        }

    }
}
